//
// Created by lovep on 2025/3/28.
//

#include "sqstack.h"

#include <cstdio>
#include <cstdlib>

Status SqStack_Init(SqStack &S) {
    S.base = (SElemType *) malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if (!S.base) {
        return ERROR;
    }

    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}


Status SqStack_Push(SqStack &S,SElemType e) {
    if (S.top - S.base >= S.stacksize) {
        return ERROR;
    }
    *S.top++ = e;
    return OK;
}


Status SqStack_Pop(SqStack &S,SElemType &e) {
    if (S.top == S.base) {
        return ERROR;
    }

    e = *--S.top;
    return OK;
}

Status SqStack_GetTop(SqStack S,SElemType &e) {
    if (S.top == S.base) {
        return ERROR;
    }

    e = *(S.top - 1);
    return OK;
}

Status SqStack_StackEmpty(SqStack S) {
    return S.top == S.base;
}

Status SqStack_Destroy(SqStack &S) {
    free(S.base);
    S.top = NULL;
    S.base = NULL;
    S.stacksize = 0;
    return OK;
}

Status SqStack_Clear(SqStack &S) {
    while (!SqStack_StackEmpty(S)) {
        SElemType _;
        SqStack_Pop(S, _);
    }
    return OK;
}

Status SqStack_Display(SqStack S) {
    int first = 1;
    printf("Bottom-");
    SElemType *temp = S.base;
    while (temp != S.top) {
        if (first) {
            first = 0;
        } else {
            printf("->");
        }
        printf("%d", *temp);
        temp++;
    }
    printf("-Top");
    return OK;
}